package com.optiktv.web.servlet.mvc;

import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;

import com.optiktv.web.jpa.bean.Monthly;
import com.optiktv.web.jpa.bean.Order;
import com.optiktv.web.jpa.dao.OrderDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * Order form controller.
 * 
 * @author Khizer Syed
 */
@Controller
public class OrderController {

    private static final String SEARCH_VIEW_KEY = "redirect:search.html";
    private static final String SEARCH_MODEL_KEY = "orders";

    @Autowired
    protected OrderDao orderDao = null;

    /**
     * For every request for this controller, this will 
     * create a Order instance for the form.
     */
    @ModelAttribute
    public Order newRequest(@RequestParam(required=false) Long ordernum) {
        return (ordernum != null ? orderDao.findOrderByOrdernum(ordernum) : new Order());
    }

    /**
     * <p>Order form request.</p>
     * 
     * <p>Expected HTTP GET and request '/order/form'.</p>
     */
    @RequestMapping(value="/order/form", method=RequestMethod.GET)
    public void form() {}
    
    /**
     * <p>Saves an order.</p>
     * 
     * <p>Expected HTTP POST and request '/order/form'.</p>
     */
    @RequestMapping(value="/order/form", method=RequestMethod.POST)
    public void form(Order order, Model model) {
        if (order.getOrderdate() == null) {
            order.setOrderdate(new Date().getTime());
        }

        Order result = orderDao.save(order);
          
        // set id from create
        if (order.getOrdernum() == null) {
            order.setOrdernum(result.getOrdernum());
        }

        model.addAttribute("statusMessageKey", "order.form.msg.success");
    }

    /**
     * <p>Deletes a Order.</p>
     * 
     * <p>Expected HTTP POST and request '/order/delete'.</p>
     */
    @RequestMapping(value="/order/delete", method=RequestMethod.POST)
    public String delete(Order order) {
        orderDao.delete(order);

        return SEARCH_VIEW_KEY;
    }

    /**
     * <p>Searches for all orders and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/order/search'.</p>
     */
    @RequestMapping(value="/order/search", method=RequestMethod.GET)
    public @ModelAttribute(SEARCH_MODEL_KEY) Collection<Order> search() {
        return orderDao.findOrders();
    }

    
    @RequestMapping(value="/order/daily/{year}/{month}", method=RequestMethod.GET)
    public @ModelAttribute(SEARCH_MODEL_KEY) ModelAndView dailyReport(@PathVariable("year") int year, @PathVariable("month") int month, Model model) {
    	Collection<Order> orders = orderDao.findOrdersByMonth(year, month);
    	Calendar cal = Calendar.getInstance();
    	cal.set(year, month, 1);
    	Date date = cal.getTime();
    	
    	
    	model.addAttribute("orders", orders);
    	ModelAndView mav = new ModelAndView("/order/daily", model.asMap());
    	return mav;
    }

    @RequestMapping(value="/order/daily", method=RequestMethod.GET)
    public @ModelAttribute(SEARCH_MODEL_KEY) Collection<Order> dailyReport() {
    	return Collections.EMPTY_LIST;
    }

    
    
    @RequestMapping(value="/order/monly/{year}", method=RequestMethod.GET)
    public @ModelAttribute(SEARCH_MODEL_KEY) String monthlyReport(@PathVariable("year") int year, Model model) {
    	
    	//Monthly report = OrderDao.monthlyReport(year);
    	//model.addAttribute("report", report);
    	return "/order/monthly";
    }

    
}
